import * as THREE from "three"
export default class LabelTextBackgroundMesh extends THREE.Mesh {
  hitTest: (s: any, r: any) => void
  labelMesh: any
  data: any
  declare material: any
  constructor() {
    super(...arguments)
    this.hitTest = (() => {
      const e = new THREE.Matrix4()
      const t = new THREE.Ray()
      const i = new THREE.Plane(new THREE.Vector3(0, 0, -1))
      const n = new THREE.Vector3()
      return (s, r) => {
        if ((e.copy(this.matrixWorld).invert(), t.copy(s.ray).applyMatrix4(e), t.intersectPlane(i, n))) {
          const e = this.scale.x / this.scale.y
          let t = 0.5
          e < 1.5 && (t = 1 / e),
            Math.abs(n.x) <= t &&
              Math.abs(n.y) <= 0.75 &&
              (n.applyMatrix4(this.matrixWorld),
              r.push({
                distance: s.ray.origin.distanceTo(n),
                point: n.clone(),
                object: this
              }))
        }
      }
    })()
  }
  labelVisible() {
    return !(!this.labelMesh || !this.labelMesh.labelVisible())
  }
  getId() {
    if (!this.data) throw new Error("LabelInputCollider used before configure")
    return this.data.sid
  }
  raycast(e, t) {
    if (!this.labelVisible()) return
    if (this.material.depthTest) return void this.hitTest(e, t)
    const i = this.material.depthTest ? t : []
    this.hitTest(e, i), i.length > 0 && ((i[0].distance /= 1e4), t.push(i[0]))
  }
}
